package com.example.luckybag

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import androidx.databinding.DataBindingUtil
import com.example.luckybag.databinding.MainLayoutBinding
import com.example.luckybag.sysservice.MyAccessibilityService
import com.example.luckybag.sysservice.MyNotificationListenerService
import com.example.luckybag.utils.Utils

/**
 * 文件名： <br/>
 * 描述：测试页面  没少好说的🌝🌝🌝
 *
 * @author wulinran
 * @since 2023/2/3 13:45
 */

class MainActivity : AppCompatActivity() {

    companion object {
        @JvmStatic
        var GET_WECHAT_LUCKY_BAG_TYPE = false
    }

    private lateinit var mBind: MainLayoutBinding


    private val serviceName: String =
        "com.example.LuckyBag/com.example.Luckybag.MyAccessibilityService"

    private val serviceRunRobName: String = "com.example.luckyBag.MyAccessibilityService"
    private val serviceRunNotificationName: String =
        "com.example.luckybag.sysservice.MyNotificationListenerService"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = DataBindingUtil.setContentView(this, R.layout.main_layout)

        mBind.statusClick.setOnClickListener {
            val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
            startActivity(intent)
        }

        mBind.userType.isChecked = GET_WECHAT_LUCKY_BAG_TYPE

        mBind.userType.setOnCheckedChangeListener { buttonView, isChecked ->
            GET_WECHAT_LUCKY_BAG_TYPE = isChecked
        }

        mBind.luckyPackageSwitch.isChecked = Utils.luckyPackageSwitch()

        mBind.luckyPackageSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            Utils.luckyPackageSwitchChange(isChecked)
        }

    }

    override fun onResume() {
        super.onResume()
        if (NotificationManagerCompat.getEnabledListenerPackages(this).contains(packageName)) {
            val intent = Intent(this, MyNotificationListenerService::class.java)

            if (!Utils.isServiceRunning(this@MainActivity, serviceRunNotificationName)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    //适配8.0机制
                    startForegroundService(intent)
                } else {
                    startService(intent)
                }
            }

        } else {
            startActivity(Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
        }

        if(!Utils.canShowNotification(this)){
            Toast.makeText(this,"请开启通知栏权限",Toast.LENGTH_SHORT).show()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val intent = Intent()
                intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
                startActivity(intent)
            }
        }


        if (Utils.enabled(this@MainActivity, serviceName)) {
            mBind.status.text = "当前状态：已授权"
            mBind.statusClick.text = "去关闭无障碍授权"

            if (!Utils.isServiceRunning(this@MainActivity, serviceRunRobName)) {
                val intent = Intent(this, MyAccessibilityService::class.java)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    //适配8.0机制
                    startForegroundService(intent)
                } else {
                    startService(intent)
                }
            }

        } else {
            mBind.status.text = "当前状态：未授权"
            mBind.statusClick.text = "去开启无障碍授权"
            val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
            startActivity(intent)
        }
    }


    //    override fun onBackPressed() {
//        moveTaskToBack(false)
//    }

}
